package com.aidex.biz.controller;

import java.util.List;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.CapBill;
import com.aidex.biz.service.CapBillService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 资方结算Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-12-17
 */
@RestController
@RequestMapping("/biz/bizCapBill")
public class CapBillController extends BaseController
{
    @Autowired
    private CapBillService capBillService;

    /**
     * 查询资方结算列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:list')")
    @GetMapping("/list")
    public R<PageInfo> list(CapBill capBill, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                capBill.setCompanyId(null);
        }else{
                     capBill.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        capBill.setPage(new PageDomain(request, response));
        return R.data(capBillService.findPage(capBill));
    }

    /**
     * 获取资方结算详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:query')")
    @GetMapping(value = "/{id}")
    public R<CapBill> detail(@PathVariable("id") String id)
    {
        return R.data(capBillService.get(id));
    }

    /**
     * 新增资方结算
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:add')")
    @Log(title = "资方结算", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  CapBill capBill)
    {
        return R.status(capBillService.save(capBill));
    }

    /**
     * 修改资方结算
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:edit')")
    @Log(title = "资方结算", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated CapBill capBill)
    {
        if("1".equals(capBill.getStatus())){
            if("2".equals(capBill.getType())){
                capBill.setAmount(capBill.getFkAmount().multiply(capBill.getLv()));
            }
        }
        return R.status(capBillService.save(capBill));
    }


    /**
     * 删除资方结算
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:remove')")
    @Log(title = "资方结算", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(capBillService.deleteCapBillByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(capBillService.findMaxSort(new CapBill()));
    }

    /**
     * 导出资方结算列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizCapBill:export')")
    @Log(title = "资方结算", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(CapBill capBill)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    capBill.setCompanyId(null);
            }else{
                         capBill.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<CapBill> list = capBillService.findList(capBill);
        ExcelUtil<CapBill> util = new ExcelUtil<CapBill>(CapBill.class);
        return util.exportExcel(list, "资方结算数据");
    }

}
